package com.dragon.graduation.roleMenu.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.dragon.util.MyPage;
import com.dragon.util.WrapperUtil;
import com.dragon.graduation.roleMenu.controller.dto.RoleMenuAddDTO;
import com.dragon.graduation.roleMenu.controller.dto.RoleMenuSearchDTO;
import com.dragon.graduation.roleMenu.controller.dto.RoleMenuUpdateDTO;
import com.dragon.graduation.roleMenu.controller.vo.RoleMenuVO;
import com.dragon.graduation.roleMenu.entity.RoleMenuEntity;
import com.dragon.graduation.roleMenu.service.RoleMenuService;
import com.dragon.config.exception.exception.GlobalException;
import com.dragon.results.page.ResultPage;
import com.dragon.results.result.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色菜单表 前端控制器
 * </p>
 *
 * @author dragon
 * @since 2025-02-28 21:21:44
 */
@RestController
@RequestMapping("/roleMenu")
@RequiredArgsConstructor
@Api(tags = "角色菜单表 相关接口")
public class RoleMenuController {

    private final RoleMenuService roleMenuService;

    @PostMapping("add")
    @ApiOperation("1]角色菜单表添加")
    @ApiOperationSupport(order = 1)
    public HttpResult<String> add(@RequestBody RoleMenuAddDTO addDTO) {
        roleMenuService.removeByRoleId(addDTO.getRoleId());
        List<RoleMenuEntity> menuEntityList=new ArrayList<>();
        for (Integer menuId : addDTO.getMenuId()) {
            RoleMenuEntity build = RoleMenuEntity.builder()
                    .roleId(addDTO.getRoleId())
                    .menuId(menuId)
                    .build();
            menuEntityList.add(build);
        }
        if (!CollectionUtils.isEmpty(menuEntityList)) {
            roleMenuService.saveBatch(menuEntityList);
        }
        return HttpResult.ok("添加成功");
    }

    @PostMapping("update")
    @ApiOperation("2]角色菜单表修改")
    @ApiOperationSupport(order = 2)
    public HttpResult<String> update(@RequestBody RoleMenuUpdateDTO updateDTO) {
        RoleMenuEntity entity = roleMenuService.getById(updateDTO.getId());
        Assert.isFalse(ObjectUtils.isEmpty(entity), () -> new GlobalException("要修改的数据不存在"));
        entity = RoleMenuUpdateDTO.to(updateDTO, RoleMenuEntity.class);
        roleMenuService.updateById(entity);
        return HttpResult.ok("修改成功");
    }

    @PostMapping("search")
    @ApiOperation("3]角色菜单表分页查询")
    @ApiOperationSupport(order = 3)
    public HttpResult<ResultPage<List<RoleMenuVO>>> search(@RequestBody RoleMenuSearchDTO searchDTO) {
        QueryWrapper<RoleMenuEntity> queryWrapper = WrapperUtil.constructWrapper(searchDTO, RoleMenuEntity.class);

        Page<RoleMenuEntity> entityPage = MyPage.setPage(RoleMenuEntity.class, searchDTO.getPage());
        Page<RoleMenuEntity> page = roleMenuService.page(entityPage, queryWrapper);
        ResultPage<List<RoleMenuVO>> resultPage = MyPage.copyPage(page, RoleMenuVO.class);
        return HttpResult.okForData(resultPage);
    }

    @PostMapping("delete")
    @ApiOperation("4]角色菜单表删除")
    @ApiOperationSupport(order = 4)
    public HttpResult<String> delete(@RequestBody List<Integer> idList) {
        roleMenuService.removeByIds(idList);
        return HttpResult.ok("删除成功");
    }
}
